import {
	useAmapGetRegeo,
	useChooseLocation,
	useWxFuzzyLocation,
	useWxGetLocation
} from "../../../../hooks/useLocation";
import {
	formKeys
} from "../report.form.config";

export default {
	data() {
		return {
			ml_markers: [],
		}
	},
	onLoad() {
		this.mlGetPosition()
	},
	methods: {
		async mlGetPosition() {
			// this.ml_markers.push({
			// 	id: new Date().getTime(), //uni.$u.guid(),
			// 	longitude: 126.65,
			// 	latitude: 45.72,
			// 	width: 20,
			// 	height: 30
			// })
			// this.form[formKeys.lat] = 45.72;
			// this.form[formKeys.lon] = 126.65;
			// this.form[formKeys.eventLocation] = "哈尔滨市测试地址(乐松)";
			// return
			const {
				latitude,
				longitude
			} = await useWxGetLocation();
			this.mlSetData({
				lat: latitude,
				lon: longitude
			})

			// const location = longitude + ',' + latitude;

			// const datas = await useAmapGetRegeo(location)
			// const {
			// 	latitude: lat,
			// 	longitude: lon,
			// 	name
			// } = datas[0]
			// console.log("useAmapGetRegeo", lat, lon, name);
			// this.ml_markers.push({
			// 	id: new Date().getTime(),
			// 	latitude: lat,
			// 	longitude: lon,
			// 	width: 20,
			// 	height: 30
			// })
			// this.form[formKeys.lat] = lat;
			// this.form[formKeys.lon] = lon;
			// this.form[formKeys.eventLocation] = name;
		},
		// 手动获取位置信息
		async mlChooseLocation() {
			const {
				latitude,
				longitude,
				address,
				name
			} = await useChooseLocation({
				lat: this.form[formKeys.lat],
				lon: this.form[formKeys.lon]
			});
			this.mlSetData({
				lat: latitude,
				lon: longitude,
				locationName: address + name
			})
		},
		// 点击地图
		mlCheckMap(e) {
			console.log("点击地图", e);
			const {
				latitude,
				longitude
			} = e.detail;
			this.mlSetData({
				lat: latitude,
				lon: longitude
			})
		},
		async mlSetData({
			lat,
			lon,
			locationName
		}) {
			try {
				const datas = await useAmapGetRegeo(lon + "," + lat)
				const {
					latitude,
					longitude,
					name,
					regeocodeData
				} = datas[0]
				// console.log("useAmapGetRegeo", latitude, longitude, name);
				// const {
				// 	location
				// } = regeocodeData.addressComponent.streetNumber;
				// const [longitude, latitude] = location.split(",");
				this.ml_markers = [];
				this.ml_markers.push({
					id: new Date().getTime(),
					latitude: lat,
					longitude: lon,
					width: 20,
					height: 30
				})
				this.form[formKeys.lat] = lat;
				this.form[formKeys.lon] = lon;
				this.form[formKeys.eventLocation] = locationName ?? name;
				this.form[formKeys.distinguish] = regeocodeData?.addressComponent?.district;
			} catch (e) {
				//TODO handle the exception
				this.$toast("地点设置失败!")
			}
		},




		// 获取当前定位
		_mlGetPosition() {
			let _this = this;
			wx.getFuzzyLocation({
				type: 'wgs84', // gcj02 wgs84
				success: function(res) {
					console.log('uni定位', res);
					_this.ml_markers.push({
						id: uni.$u.guid(),
						latitude: res.latitude,
						longitude: res.longitude
						// longitude: 126.65,
						// latitude: 45.72
					})
					_this.form[formKeys.lat] = res.latitude;
					_this.form[formKeys.lon] = res.longitude;
					const location = res.longitude + ',' + res.latitude;
					// const key = _this.$basic_config.amapKey;
					// uni.request({
					// 	url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=${key}`,
					// 	data: {},
					// 	success: (ress) => {
					// 		console.log('地址解析', ress);
					// 		if (ress.errMsg === 'request:ok') {
					// 			_this.form[formKeys.eventLocation] = ress.data.result.address;
					// 		} else _this.$toast('地址解析失败!')

					// 	}
					// })
				},
				fail(err) {
					console.log('uni定位', err);
					// _this.loadLocation = '[fail]:' + JSON.stringify(err);
				}
			})










			// let _this = this;
			// uni.getLocation({
			// 	type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回国测局坐标，可用于 uni.openLocation 和 map 组件坐标，App 和 H5 需配置定位 SDK 信息才可支持 gcj02
			// 	geocode: true,
			// 	success(res) {
			// 		console.log('获取位置', res);
			// 		if (res.errMsg === "getLocation:ok") {
			// 			_this.$toast('已更新定位')
			// 			// _this.ml_markers[0].longitude = res.longitude;
			// 			// _this.ml_markers[0].latitude = res.latitude;
			// 			// _this.form[formKeys.lat] = res.latitude;
			// 			// _this.form[formKeys.lon] = res.longitude;

			// 			_this.ml_markers.push({
			// 				longitude: 126.65,
			// 				latitude: 45.72
			// 			})

			// 			// _this.ml_markers[0].longitude = 126.65;
			// 			// _this.ml_markers[0].latitude = 45.72;
			// 			_this.form[formKeys.lat] = 45.72;
			// 			_this.form[formKeys.lon] = 126.65;

			// 			// _this.getSign();
			// 		} else _this.$toast('定位失败')
			// 	},
			// 	fail(err) {
			// 		console.log('获取位置err', err);
			// 		_this.$toast('请勿频繁调整,请稍后再试')
			// 		// _this.form[formKeys.lat] = 45.72919;
			// 		// _this.form[formKeys.lon] = 126.60756;
			// 		// _this.form[formKeys.lat] = 32.043644;
			// 		// _this.form[formKeys.lon] = 118.822585;
			// 		_this.ml_markers.push({
			// 			longitude: 126.65,
			// 			latitude: 45.72
			// 		})
			// 		_this.form[formKeys.lat] = 45.72;
			// 		_this.form[formKeys.lon] = 126.65;
			// 		// 126.65,45.72
			// 		_this.form[formKeys.eventLocation] = "哈尔滨市测试地址(乐松)";
			// 		// [[118.822585, 32.043644], [118.835245, 32.043717], [118.832198, 32.035531]]
			// 	}
			// })
		},

	}
}